import { PartialType, PickType } from "@nestjs/swagger";
import { DtoValidation } from "src/Core/dto.validatorpip";
import { UserCommonDto } from "./user.comment.dto";
import { IsDefined, IsEnum, IsNumber, IsOptional, IsUUID, Length, Min } from "class-validator";
import { UserOrderType } from "../helper";
import { Transform } from "class-transformer";
import {toNumber} from 'lodash'
@DtoValidation()
export class CreateUserDto extends PickType(UserCommonDto, [
    'username',
    'nickname',
    'password',
    'phone',
    'email',
]) {}

@DtoValidation( )
export class UpdateUserDto extends PartialType(CreateUserDto) {
  
    @IsUUID(undefined, { message: '用户ID格式不正确' })
    @IsDefined({ message: '用户ID必须指定' })
    id!: string;
}

@DtoValidation({ type: 'query' })
export class QueryUserDto  {
    
    @Transform(({ value }) => toNumber(value))
    @Min(1, { message: '当前页必须大于1' })
    @IsNumber()
    @IsOptional()
    page = 1;

    @Transform(({ value }) => toNumber(value))
    @Min(1, { message: '每页显示数据必须大于1' })
    @IsNumber()
    @IsOptional()
    limit = 10;
    @IsEnum(UserOrderType)
    orderBy?: UserOrderType;
}

export class UpdatePasswordDto extends PickType(UserCommonDto, ['password', 'plainPassword']) {
  
    // @IsPassword(5, {
    //     message: '密码必须由小写字母,大写字母,数字以及特殊字符组成',
    //     always: true,
    // })
    @Length(8, 50, {
        message: '密码长度不得少于$constraint1',
        always: true,
    })
    oldPassword!: string;
}

export class CredentialDto extends PickType(UserCommonDto, ['credential', 'password']) {}

@DtoValidation()
export class RegisterDto extends PickType(UserCommonDto, [
    'username',
    'nickname',
    'password',
    'plainPassword',
] as const) {}
// 用于将数组 ['username', 'nickname', 'password', 'plainPassword'] 的每个元素都标记为常量，